//
//  UIImageView+DR.m
//  DoctorRen
//
//  Created by Macx on 2018/11/20.
//

#import "UIImageView+DR.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import <AVFoundation/AVAsset.h>
#import <AVFoundation/AVAssetImageGenerator.h>
#import <AVFoundation/AVTime.h>

@implementation UIImageView (DR)

-(void)setImageWithURLString:(NSString *)url{
    [self sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"placeholder"]];
}

-(UIImage *)setVideoPreViewImage:(NSString *)url{
    NSURL *path = [NSURL URLWithString:url];
    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:path options:nil];
    AVAssetImageGenerator *assetGen = [[AVAssetImageGenerator alloc] initWithAsset:asset];
    
    assetGen.appliesPreferredTrackTransform = YES;
    CMTime time = CMTimeMakeWithSeconds(0.0, 600);
    NSError *error = nil;
    CMTime actualTime;
    CGImageRef image = [assetGen copyCGImageAtTime:time actualTime:&actualTime error:&error];
    UIImage *videoImage = [[UIImage alloc] initWithCGImage:image];
    CGImageRelease(image);
//    self.image = videoImage;
    return videoImage;
}

@end
